﻿using System;
using System.Collections.Generic;

namespace SmartSpace.Silverlight
{
    public static class AppModuleManager
    {
        private static Dictionary<Type, AppModule> s_appModules = new Dictionary<Type,AppModule>();

        /// <summary>
        /// 注册应用模块
        /// </summary>
        /// <param name="appModule"></param>
        public static void RegisterModule(AppModule appModule)
        {
            Type type = appModule.GetType();

            if (s_appModules.ContainsKey(type))
            {
                return;
            }
            s_appModules.Add(type, appModule);
        }

        internal static AppModule GetModule(Type type)
        {
            if (!s_appModules.ContainsKey(type))
            {
                return null;
            }

            return s_appModules[type];
        }

        public static AppModule GetModule(Guid moduleID)
        {
            foreach (AppModule module in s_appModules.Values)
            {
                if (module.ID == moduleID)
                {
                    return module;
                }
            }
            return null;
        }

        public static AppView GetAppView(int viewID)
        {
            foreach (AppModule module in s_appModules.Values)
            {
                AppView appView = module.GetAppView(viewID);
                if (appView != null)
                {
                    return appView;
                }
            }

            return null;
        }
    }
}
